package C038;

import C017.main;

import java.util.Arrays;
import java.util.HashSet;

/**
 * ClassName: C01
 * Package: C038
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/1/29 20:08
 * @Version 1.0
 * @Since 1.0
 */
public class C01 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(generatePermutation("abc")));
    }


    public static String[] generatePermutation (String s) {
        // write code here
        HashSet<String> ans = new HashSet<>();
        HashSet<String> ans2 = new HashSet<>();
        process(s.toCharArray(), new StringBuilder(""), 0, ans);
        char[] charArr = s.toCharArray();
        process2(charArr, 0, new char[charArr.length], 0, ans2);

        int size = ans.size();
        int i = 0;
        String[] ansStrArr = new String[size];
        for (String an : ans2) {
            ansStrArr[i] = an;
            i ++;
        }
        return ansStrArr;
    }

    private static void process2(char[] charArr, int path, char[] chars, int index, HashSet<String> ans) {
        if(path == chars.length) {
            ans.add(String.valueOf(chars, 0, index));
            return;
        }

        chars[index] = charArr[path];
        process2(charArr, path+1, chars, index+1, ans);
        process2(charArr, path+1, chars, index, ans);

    }

    private static void process(char[] arr, StringBuilder stringBuilder, int path, HashSet<String> ans) {
        if(path == arr.length) {
            ans.add(stringBuilder.toString());
            return;
        }

        // 别的路径
        stringBuilder.append(arr[path]);
        process(arr, stringBuilder, path+1, ans);
        stringBuilder.deleteCharAt(stringBuilder.length()-1);
        process(arr, stringBuilder, path+1, ans);
    }

}
